package com.example.demo.controller;

import com.example.demo.pojo.AirQualityIndex;
import com.example.demo.pojo.District;
import com.example.demo.service.AirQualityIndexService;
import com.example.demo.service.DistrictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class TestController {
   @Autowired
    AirQualityIndexService airQualityIndexService;
   @Autowired
    DistrictService districtService;
   @RequestMapping("toshow")
    private String toshow(){
       return "index";
   }
   @RequestMapping("getdistricts")
   @ResponseBody
   private List<District> getdistricts(){
       return districtService.getAllDistrict();
   }
    @RequestMapping("getairbyid")
    @ResponseBody
    private AirQualityIndex getairbyid(Integer id){
        return airQualityIndexService.getAriQualityById(id);
    }
    @RequestMapping("getairs")
    @ResponseBody
    private Object getairs(AirQualityIndex airQualityIndex, @RequestParam(defaultValue = "1") Integer pageNo,@RequestParam(defaultValue = "5") Integer pageSize){
    if (airQualityIndex.getDistrictid()==null||airQualityIndex.getDistrictid()==0){
               airQualityIndex.setDistrictid(null);
            }
    PageHelper.offsetPage((pageNo-1)* pageSize, pageSize);
    List<AirQualityIndex> airQualityIndices=airQualityIndexService.getAriQualityByContent(airQualityIndex);
    PageInfo<AirQualityIndex> pageInfo= new PageInfo<>(airQualityIndices);
    return pageInfo;
}
    @RequestMapping("addair")
    @ResponseBody
    private String addair(AirQualityIndex airQualityIndex){
        System.out.println(airQualityIndex);
       int num=airQualityIndexService.addAriQuality(airQualityIndex);
       return num>0?"ok":"no";
    }
    @RequestMapping("updateair")
    @ResponseBody
    private String updateair(AirQualityIndex airQualityIndex){
        int num=airQualityIndexService.changeAriQuality(airQualityIndex);
        return num>0?"ok":"no";
    }
    @RequestMapping("removeair")
    @ResponseBody
    private String removeair(Integer id){
        int num=airQualityIndexService.removeAriQuality(id);
        return num>0?"ok":"no";
    }
}
